package com.boot2.core.feign;

import com.boot2.core.utils.JwtUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.util.CollectionUtils;

import java.util.Collection;

/**
* @author zhangweilin
* @date 2019/5/30 22:14 
* @Description:  feign 拦截，
 * 1：添加是feign请求的标识，下游接收到请求时，从头的key=feign里获取是否有值，如果有则为feign请求，可以把异常原本往上抛
 * 2: 将原token续传到下游
*/
@CommonsLog
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {

    public static final String headerKey = "feign";

    @Override
    public void apply(RequestTemplate requestTemplate) {
        //feign标识
        requestTemplate.header(headerKey, "true");
        Collection<String> xAccessTokenList = requestTemplate.queries().get(JwtUtil.X_ACCESS_TOKEN);
        if (!CollectionUtils.isEmpty(xAccessTokenList)) {
            //续传token
            requestTemplate.header(JwtUtil.X_ACCESS_TOKEN, xAccessTokenList.iterator().next());
        }else{

        }
    }
}